﻿using EmailManagementSoftware.Model;
using EmailManagementSoftware.Presenter;
using EmailManagementSoftware.View.Interfaces;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace EmailManagementSoftware
{
    public partial class MainWindowView : Form, IMainWindowView
    {
        public AppPresenter _AppPresenter { get; set; }


        public MainWindowView()
        {
            InitializeComponent();
        }

        public MainWindowView(AppPresenter appPresenter) : this()
        {
            this._AppPresenter = appPresenter;
        }


        private void btnManageYourEmailCredentials_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnManageYourEmailCredentials_Click(sender,e);
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnLogIn_Click(sender, e);
        }

        private void btnLogOut_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnLogOut_Click(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnAdd_Click(sender, e);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnEdit_Click(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnDelete_Click(sender, e);
            if (this.clbEmailsSubjects.Items.Count == 0)
                btnDelete.Enabled = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            _AppPresenter.btnSend_Click(sender, e);
        }

        private void clbEmailsSubjects_LostFocus(object sender, EventArgs e)
        {
            clbEmailsSubjects.ClearSelected();
        }


        public void updateGUIAfterLogIn(List<EmailMessage> emailMessages)
        {
            pnlUserCredentials.Visible = false;

            //update the Welcome message
            lblWelcome.Text += _AppPresenter._LoggedInUser._username + "!";
            pnlWelcome.Visible = true;


            if (emailMessages != null)
            {
                int count = emailMessages.Count;
                for (var i = 0; i < count; i++)
                {
                    clbEmailsSubjects.Items.Add(emailMessages[i]);
                } 
            }


            //becomes visible after beeing populated or checkBoxList remaining empty
            pnlEmails.Visible = true;
        }


        public void updateGUIAfterLogOut()
        {
            pnlUserCredentials.Visible = true;
            pnlWelcome.Visible = false;
            pnlEmails.Visible = false;

            resetGUIForFutureLogIn();
        }


        //TODO reseteaza inclusiv progress bar
        public void resetGUIForFutureLogIn()
        {
            lblWelcome.Text = "Welcome, ";

            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnSend.Enabled = false;

            clbEmailsSubjects.Items.Clear();
        }

        private void clbEmailsSubjects_Click(object sender, EventArgs e)
        {
            _AppPresenter.clbEmailsSubjects_Click(sender, e);
        }

        
        public void updateGUIOnclbEmailsSubjectsClick()
        {
            BeginInvoke((Action)(() =>
            {
                btnEdit.Enabled = (clbEmailsSubjects.CheckedItems.Count == 1);
                btnDelete.Enabled = (clbEmailsSubjects.CheckedItems.Count >= 1);
            }));
        }


        public string _Username
        {
            get { return tbxUser.Text; }
        }

        public string _Password
        {
            get { return tbxPassword.Text; }
        }


        public EmailMessage checkedEmailMessage
        {
            get { return clbEmailsSubjects.CheckedItems[0] as EmailMessage; }
        }


        public void repopulateClbEmailsSubjects(List<EmailMessage> emailMessages)
        {
            if (emailMessages != null)
            {

                int count = emailMessages.Count;
                for (var i = 0; i < count; i++)
                {
                    clbEmailsSubjects.Items.Add(emailMessages[i]);
                }
            }
            else
            {
                clbEmailsSubjects.Items.Clear();
            }
        }

        public void updateStateWhenCheckedListBoxFocusIsLost()
        {
            clbEmailsSubjects.ClearSelected();

            /*foreach (int i in clbEmailsSubjects.CheckedIndices)
            {
                clbEmailsSubjects.SetItemCheckState(i, CheckState.Unchecked);
            }*/
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnSend.Enabled = false;
        }

        private void clbEmailsSubjects_Leave(object sender, EventArgs e)
        {
            clbEmailsSubjects.ClearSelected();
        }
    }
}
